package server.phone.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONObject;
import server.phone.dao.CashierDAO;

import com.opensymphony.xwork2.ActionSupport;
import com.utopia84.model.Cashier;
import com.utopia84.model.Cashierrecord;

public class GetCashierRecordServlet extends ActionSupport {
	private static final long serialVersionUID = -7811568044252827351L;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	public GetCashierRecordServlet(){
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
	}
	public void doGet( )	throws  IOException {

		 
		CashierDAO cd;
		List<Cashierrecord> cashierR = null; // 从数据库读取订单记录
		JSONObject jObject = new JSONObject();
		JSONObject jData = new JSONObject();
		String name = request.getParameter("cashierId");
		String con=request.getParameter("bytime");
		if(con.equals("bytime")){
			try {
				String time=request.getParameter("time");
				cd = new CashierDAO();
				cashierR = cd.getCasherRecordByTime(name,time);
				jData.put("CashierRecord", cashierR);
				jObject.put("ret", "success");
				jObject.put("data", jData);
			} catch (ClassNotFoundException e) {
				jObject.put("ret", "error");
				jObject.put("data", "");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else{
			try {
				cd = new CashierDAO();
				cashierR = cd.getCasherRecord(name);
				jData.put("CashierRecord", cashierR);
				jObject.put("ret", "success");
				jObject.put("data", jData);
			} catch (ClassNotFoundException e) {
				jObject.put("ret", "error");
				jObject.put("data", "");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		PrintWriter out = response.getWriter();
		System.out.println(jObject.toString());
		out.println(jObject);
		out.flush();
		out.close();

	}

}